package com.moli.iov.alarm.service.stat.impl;

import com.moli.iov.alarm.model.dto.req.AlarmInfoReqDto;
import com.moli.iov.alarm.model.dto.req.StatAlarmParamDto;
import com.moli.iov.alarm.model.dto.res.AlarmInfoResDto;
import com.moli.iov.alarm.service.IAlarmService;
import com.moli.iov.alarm.service.stat.AlarmStatDetailFactory;
import com.moli.iov.alarm.service.stat.IAlarmStatDetailService;
import com.moli.iov.base.remote.IVehicleBaseRemoteService;
import com.moli.iov.handle.alarm.enums.AlarmCategoryEnum;
import com.moli.iov.handle.alarm.enums.AlarmStatusEnum;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AlarmStatDetailServiceImpl implements IAlarmStatDetailService {

    @Resource
    IAlarmService alarmService;

    @Reference(group = "${service.group}", check = false)
    IVehicleBaseRemoteService vehicleBaseRemoteService;

    @Override
    public RestResponse getStatAlarmDetail(StatAlarmParamDto dto) {
        //查询所有报警详情
        AlarmInfoReqDto param = new AlarmInfoReqDto();
        if(dto!=null && dto.getAlarmCategory()!=null){
            param.setCompanyId(dto.getCompanyId());
            param.setBeginTime(dto.getBeginTime());
            param.setEndTime(dto.getEndTime());

            if(dto.getAlarmCode() == null){
                param.setAlarmCodes(new ArrayList<>(AlarmCategoryEnum.getAlarmCodes(dto.getAlarmCategory())));
            }else{
                param.setAlarmCode(dto.getAlarmCode());
            }

            if(dto.getStatus() == null){
                List<Integer> statues = Arrays.asList(AlarmStatusEnum.ALARMING.getFlag(), AlarmStatusEnum.ALARM_END.getFlag(), AlarmStatusEnum.IGNORE.getFlag());
                param.setStatues(statues);
            }else {
                param.setStatus(dto.getStatus());
            }

            if(dto.getVin() != null){
                param.setVin(dto.getVin());
            }

            if(dto.getLicense() != null){
                param.setLicense(dto.getLicense());
            }

            if(dto.getAlarmLevel() != null){
                param.setAlarmLevel(dto.getAlarmLevel());
            }

        }
        List<AlarmInfoResDto> list = alarmService.listAlarmInfo(param);
        int count = (list == null ? 0 : list.size());
        log.info("统计报警 报警详情数量：{}",count);

        //通过工厂统计报警
        Map<String, Object> result = new AlarmStatDetailFactory(list)
                                            .setVehicleTypes(vehicleBaseRemoteService,dto)
                                            .getMap(dto);
        System.out.println(JsonUtils.toJson(RestResponse.success(result)));
        return RestResponse.success(result);
    }

}
